package com.ebupt.migu.music.autoTri.Controller;

import com.ebupt.migu.music.autoTri.Service.AutoOperateService;
import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 自动运营
 * @author apple
 */
@RestController
@RequestMapping("/autoOperate")
public class AutoOperateController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private AutoOperateService autoOperateService;
    @Autowired
    private IDAuthUtils idAuthUtils;

    /**
     * 创建运营计划
     *
     * @return
     */
    @RequestMapping("/createOperPlan")
    public ResultObject createOperPlan() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.createOperPlan(params);
    }

    /**
     * 更新运营计划
     *
     * @return
     */
    @RequestMapping("/updateOperPlan")
    public ResultObject updateOperPlan() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.updateOperPlan(params);
    }

    /**
     * 查询运营计划列表
     *
     * @return
     */
    @RequestMapping("/queryOperPlan")
    public ResultObject queryOperPlan() {
        return autoOperateService.queryOperPlan();
    }

    /**
     * 创建自动触发策略
     *
     * @return
     */
    @RequestMapping("/createAutoOperate")
    public ResultObject createAutoOperate(@RequestBody Map<String,Object> map) {
        // 获取用户信息和业务参数
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        map.put("cid",params.get("cid").toString());
        map.put("creatorId",params.get("creatorId").toString());
        map.put("creatorName",params.get("creatorName").toString());
        map.put("creatorDepartmentId",params.get("creatorDepartmentId").toString());
        map.put("creatorDepartment",params.get("creatorDepartment").toString());
        map.put("creatorPhone",params.get("creatorPhone").toString());
        map.put("creatorEmail",params.get("creatorEmail").toString());
        // 创建自动运营就选择1，创建人群就选择2
        if (map.get("createType").toString().equals("1")){
            //创建自动运营
            return autoOperateService.createAutoOperate(map);
        }else {
            //创建人群
            return autoOperateService.createAutoUserGroup(map);
        }
    }

    /**
     * 查询策略列表
     *
     * @return
     */
    @RequestMapping("/queryAutoTris")
    public ResultObject queryAutoTris() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoOperateService.queryAutoTris(params);
    }

    /**
     * 查询审批策略列表 --自动运营
     *
     * @return
     */
    @RequestMapping("/queryAutoTrisCheck")
    public ResultObject queryAutoTrisCheck() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoOperateService.queryAutoTrisCheck(params);
    }

    /**
     * 查看指定策略详情
     *
     * @return
     */
    @RequestMapping("/queryAutoTriDetail")
    public ResultObject queryAutoTriDetail() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.queryAutoTriDetail(params);
    }

    /**
     * 修改策略详情-自动运营
     *
     * @return
     */
    @PostMapping("/updateAutoTri")
    public ResultObject updateAutoTri(@RequestBody Map<String,Object> map) {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdminOrAuditorPost(request,map)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权操作此策略");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        map.put("creatorId",params.get("creatorId").toString());
        map.put("creatorName",params.get("creatorName").toString());
        map.put("creatorDepartmentId",params.get("creatorDepartmentId").toString());
        map.put("creatorDepartment",params.get("creatorDepartment").toString());
        map.put("creatorPhone",params.get("creatorPhone").toString());
        map.put("creatorEmail",params.get("creatorEmail").toString());
        return autoOperateService.updateAutoTri(map);
    }

    /**
     * 暂停策略 -- 自动运营
     *
     * @return
     */
    @RequestMapping("/pause")
    public ResultObject pause() {
        // 验证用户是否有此操作权限
        if (!idAuthUtils.isCreatorOrAdmin(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权操作此策略");
        }
        // 获取用户信息和业务参数
        Map<String, Object> params = AuthBaseUtils.authBaseInfo(request);
        return autoOperateService.pause(params);
    }

    /**
     * 策略审批
     *
     * @return
     */
    @RequestMapping("/doApprove")
    public ResultObject doApprove() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoOperateService.doApprove(params);
    }

    /**
     * 查询策略审批列表
     *
     * @return
     */
    @RequestMapping("/queryApproveRecords")
    public ResultObject queryApproveRecords() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return autoOperateService.queryApproveRecords(params);
    }

    /**
     * 查询指定策略审批记录
     *
     * @return
     */
    @RequestMapping("/queryApproveRecordByTacID")
    public ResultObject queryApproveRecordByTacID() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.queryApproveRecordByTacID(params);
    }

    /**
     * 撤回策略--自定运营
     * <br>
     * 在创建策略后审批前，提供撤回策略选项，撤回后将策略状态改为待提审
     *
     * @return
     */
    @GetMapping("/revocation")
    public ResultObject<String> revocation() {
        // 验证登录账号是否能执行该操作
        if (!idAuthUtils.isCreatorOrAdminOrAuditor(request)) {
            throw new MyException(StatusEnum.OPERATE_LIMITED, "用户无权撤回此策略");
        }
        return autoOperateService.revocation(ReqestParamUtil.getParamsWithUserInfo(request));
    }

    /**
     * 实时测试
     *
     * @return
     */
    @RequestMapping("/manual")
    public ResultObject manual() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.manual(params);
    }

    /**
     * 获取行为列表
     *
     * @return
     */
    @PostMapping("/selectList")
    public ResultObject selectList() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.selectList();
    }

    /**
     * 删除策略,只有已经驳回的可以删除
     *
     * @return
     */
    @GetMapping("/deleteStatus")
    public ResultObject deleteStatus() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.deleteStatus(params);
    }

    /**
     * 查看自动运营修改回填的Json
     *
     * @return
     */
    @RequestMapping("/queryAutoTriJson")
    public ResultObject queryAutoTriJson() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.queryAutoTriJson(params);
    }

    /**
     * 审批策略，通过和不通过
     *
     * @return
     */
    @GetMapping("/approvalStatus")
    public ResultObject approvalStatus() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return autoOperateService.approvalStatus(params);
    }

    /**
     * 人群列表查询人群规模
     *
     * @return
     */
    @GetMapping("/userGroupInfo")
    public ResultObject approvalStatus(@RequestParam String groupId) {
        return autoOperateService.userGroupInfo(groupId);
    }
}
